package com.am.mosl.cityandroid.leftmenu;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.am.mosl.cityandroid.R;
import com.am.mosl.cityandroid.activity.BaseActivity;
import com.am.mosl.cityandroid.bean.AccountBean;
import com.am.mosl.cityandroid.constant.HttpConstant;
import com.am.mosl.cityandroid.message.LoginMessage;
import com.am.mosl.cityandroid.util.SettingUtils;

import de.greenrobot.event.EventBus;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by mosl on 14-7-2. 留言
 */
public class FeedBackActivity extends BaseActivity {

	public static final String TAG = "FeedBackActivity";

	private View mBack;
	private Button mNext;
	private EditText mUserMessage;
	private FinalHttp mFinalHttp = new FinalHttp();
	private int mUserId = 0;
	private ProgressDialog mDialog = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.feedback_layout);
		initBar();
		initViews();
	}

	private void initViews() {
		mNext = (Button) findViewById(R.id.login_next);
		mUserMessage = (EditText) findViewById(R.id.user_message_edit);
		mNext.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				summitMessage();
			}
		});

	}

	private void summitMessage() {

		Log.d(TAG, "summitMessage");
		mDialog = ProgressDialog.show(FeedBackActivity.this, "", "正在提交", true);
		String userMessage = mUserMessage.getText().toString().trim();
		if (userMessage.length() == 0) {
			Toast.makeText(FeedBackActivity.this, "不能为空或空格", Toast.LENGTH_SHORT)
					.show();
			mDialog.dismiss();
			return;
		}
		AjaxParams params = new AjaxParams();
		if (SettingUtils.IsLogin(FeedBackActivity.this)) {
			AccountBean accountBean = new AccountBean();
			accountBean = SettingUtils.getUserInfo(this);
			String token = accountBean.getToken();
			params.put("token", token);
		}
		params.put("message", userMessage);
		mFinalHttp.post(HttpConstant.UserLeaveMessage, params,
				new AjaxCallBack<Object>() {

					@Override
					public void onFailure(Throwable t, int errorNo,
							String strMsg) {
						super.onFailure(t, errorNo, strMsg);
						mDialog.dismiss();
						Toast.makeText(FeedBackActivity.this, "网络未连接",
								Toast.LENGTH_SHORT).show();
					}

					@Override
					public void onSuccess(Object o) {
						super.onSuccess(o);
						try {
							JSONObject object = new JSONObject(o.toString());
							Log.d(TAG, object.toString());
							mDialog.dismiss();

							String status = (String) object.opt("status");
							String msg = (String) object.opt("msg");
							if (status.equals("1")) {

								Toast.makeText(FeedBackActivity.this, "留言成功",
										Toast.LENGTH_SHORT).show();
								FeedBackActivity.this.finish();

							} else if (status.equals("2")) {

								SettingUtils
										.RemoveAccount(FeedBackActivity.this);
								LoginMessage message = new LoginMessage();
								message.setType(LoginMessage.QUIT);
								EventBus.getDefault().post(message);

								Toast.makeText(FeedBackActivity.this,
										"帐号验证失败，请重新登录", Toast.LENGTH_SHORT)
										.show();
								FeedBackActivity.this.finish();
							} else {

								Toast.makeText(FeedBackActivity.this,
										"留言失败：" + msg, Toast.LENGTH_SHORT)
										.show();
								FeedBackActivity.this.finish();

							}

						} catch (JSONException e) {
							e.printStackTrace();
						}

					}
				});
	}

	private void initBar() {
		mBack = (View) findViewById(R.id.layout_action_bar_back);
		mBack.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				FeedBackActivity.this.onBack();
			}
		});
		TextView title = (TextView) findViewById(R.id.tittle);
		title.setText("给我留言");
		TextView right_title = (TextView) findViewById(R.id.right_tittle);
		right_title.setVisibility(View.GONE);
	}

}